package com.jorado.metrics.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApplicationHelp {

    public static final Logger logger = LoggerFactory.getLogger(ApplicationHelp.class);

    private static String appName = "UNKNOWN_APPLICATION";
    private static String cluster = "DEFAULT";
    private static String hostName = "UNKNOWN_HOST";
    private static String ipAddress = IPResolver.getIP();

    static {
        try {
            // appName
            String path = System.getProperty("user.dir");
            logger.info("ApplicationHelp - user.dir = " + path);
            if (path.contains("/")) {
                String[] pathArray = path.split("/");
                if (pathArray.length > 1) {
                    ApplicationHelp.appName = pathArray[pathArray.length - 2];
                }
            }
            String appName = System.getProperty("APP_NAME");
            if (appName != null && !"".equals(appName)) {
                ApplicationHelp.appName = appName;
            }
            logger.info("ApplicationHelp.appName = " + ApplicationHelp.appName);

            // hostName
            String hostName = System.getenv("HOSTNAME");
            logger.info("ApplicationHelp.env.HOSTNAME = " + hostName);
            if (hostName != null && !"".equals(hostName)) {
                ApplicationHelp.hostName = hostName;
            }
            logger.info("ApplicationHelp.hostName = " + ApplicationHelp.hostName);

            // cluster
            String cluster = System.getenv("CLUSTER");
            logger.info("ApplicationHelp.env.CLUSTER = " + cluster);
            if (cluster != null && !"".equals(cluster)) {
                ApplicationHelp.cluster = cluster;
            }
            logger.info("ApplicationHelp.cluster = " + ApplicationHelp.cluster);

            LabelHelp.DEF_VALUES[0] = (ApplicationHelp.cluster);
            LabelHelp.DEF_VALUES[1] = (ApplicationHelp.appName);
            LabelHelp.DEF_VALUES[2] = (ApplicationHelp.hostName);
            LabelHelp.DEF_VALUES[3] = (ApplicationHelp.ipAddress);
        } catch (Throwable e) {
            logger.error(e.getMessage());
        }
    }

    public static String getAppName() {
        return appName;
    }

    public static String getCluster() {
        return cluster;
    }

    public static String getHostName() {
        return hostName;
    }

    public static String getIpAddress() {
        return ipAddress;
    }
}
